/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.repository.impl.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.plugger.infrastructure.repositorybase.IWorkspace;

/**
 *  A hibernate atomic workspace.<BR/>
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public class HWorkspace implements IWorkspace {
    private Session session;
    private Transaction transx;

    public HWorkspace() {
        session = HConfig.getSessionfactory().openSession();
        session.disconnect();
        session.setFlushMode(FlushMode.MANUAL);
    }

    public void dispose() {
        this.close();
        this.clean();
        this.session.close();
//        this._session.dispose();
    }

    public void makePersistent(Object o) {
        session.save(o);
    }

    public void persistAll() {
//        if (_session.isConnected() == false)
//            _session.reconnect();
        session.flush();

        if (transx != null) {
            transx.commit();
            transx = null;
        }
    }

    @Override
    public <T> T getById(Class<T> typeToGet, Serializable idValue) {
//        if (_session.isConnected() == false)
//            _session.reconnect();
        return (T) session.load(typeToGet, idValue);
    }

    @Override
    public <T> List<T> getAll(Class<T> typeOfResult) {
//        if (_session.isConnected() == false)
//        	_session.reconnect();
        return (List<T>) session.createCriteria(typeOfResult).list();

    }

    @Override
    public void delete(Object o) {
        session.delete(o);
    }

    @Override
    public void clean() {
        session.clear();
    }

    @Override
    public void close() {
        rollback();
        if (session.isConnected() == true) {
            session.disconnect();
        }

    }

    @Override
    public void reconnect() {
//		if(_session.isConnected() == false)
//			_session.reconnect();
    }

    public Session getSession() {
//		if(_session.isConnected() == false)
//			_session.reconnect();
        return session;
    }

    @Override
    public void beginTransaction() {
        transx = session.beginTransaction();
    }

    @Override
    public void commit() {
        transx.commit();
    }

    @Override
    public void rollback() {
        if (transx != null) {
            transx.rollback();
            transx = null;
        }
    }

    public void disconnect() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void lock(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
